<template>
  <forms ref="forms" :config="config" @submit:forms="onsubmit" :btns="btns">
  </forms>
</template>

<script>
import {_config} from "@/js/s94_tool.js";
import s94Mjs from "s94-js";
import {iframe} from "@/js/tool.js";
import {ajax} from "@/js/api_tool.js";

export default {
  props: ['__iframe'],
  data(){
    let id = this.$get('id'), btns = undefined;
    let comment_status_map = _config('status_alias_map.wechat_article.comment_status');
    let forms_config = {
      title: {label:'标题',name:'title',type:'text_one',tips:'文章标题'},
      thumb_media_id: {label:'头图',name:'thumb_media_id',type:'wechat_file',config:'jpg,jpeg,png,gif,webp',tips:'选择微信图片素材作为文章头图',value: ''},
      author: {label:'作者',name:'author',type:'text_one',tips:''},
      digest: {label:'简介',name:'digest',type:'text_more',tips:'如果为空，会截取文章内容的前面部分'},
      content: {label:'文章内容',name:'content',type:'editor_wechat',tips:''},
      content_source_url: {label:'原文地址',name:'content_source_url',type:'text_one',tips:'文章原文地址'},
      comment_status: {label:'评论类型',name:'comment_status',type:'radio',config:comment_status_map,value:0},
    }
    if (id){
      forms_config.create_time = {label:'创建时间',name:'create_time',tips:''};
    }
    if (this.$get('is_info')){
      s94Mjs.each(forms_config, function (row){ row.is_info = true; });
      btns = [];
    }
    return {
      id: id,
      btns: btns,
      config: forms_config,
    }
  },
  methods: {
    onsubmit(formData){
      let url = this.id ? `/admin/wechat_article/edit?id=`+this.id : '/admin/wechat_article/add';
      ajax({url:url, method:'POST', data:formData}, (res)=>{
        if (this.__iframe){
          iframe.exe('wechat_article/index', '$refs.list_url.list');
          iframe.hide('wechat_article/edit');
          if (!this.id) this.$refs['forms'].init();
        }else {
          this.$router.push('index');
        }
      }, (res)=>{
        if (res.errors) this.$refs['forms'].errors = res.errors;
      })

    }
  },
  mounted() {
    let app = this;
    if (!this.__iframe) window['app'] = this;
    if (this.id) {
      ajax({url:'/admin/wechat_article/info?id='+this.id, cache:true}, (res)=>{
        this.$refs['forms'].init(res.data);
        return false;
      }, ()=>{
        if (this.__iframe){
          iframe.remove('wechat_article/edit');
        }else {
          this.$router.push('index');
        }
      })
    }

    //其他初始化操作
  }
}
</script>
